package com.jobcn;

import com.jobcn.common.util.StringUtil;
import com.jobcn.dic.SpiderDic;
import com.jobcn.service.SpringUtil;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.scheduling.annotation.EnableAsync;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@EnableAsync
public class WebApplication extends SpringBootServletInitializer implements CommandLineRunner {

    /**
     * 兼容war包运行
     *
     * @param application
     * @return
     */
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }

    //这里放启动后就执行的任务
    @Override
    public void run(String... args) throws Exception {
        InputStream is=this.getClass().getResourceAsStream("./src/main/webapp/Area.json");
        Map<String,Object> areaMap= StringUtil.ReadJsonFile("./src/main/webapp/Area.json");
        Map<String,Object> areaTownMap= StringUtil.ReadJsonFile("./src/main/webapp/AreaTown.json");
        Map<String,String> areaCnMap =(Map<String,String>)areaMap.get("cn");
        Map<String,String> areaTownCnMap =(Map<String,String>)areaTownMap.get("cn");
        Map<String, Map<String,String>> newMap =new HashMap<>();
        for (Map.Entry<String, String> entry : areaCnMap.entrySet()) {
            String shiCode = entry.getKey();
            for (Map.Entry<String, String> entryTown : areaTownCnMap.entrySet()) {
                if (entryTown.getKey().startsWith(shiCode)){
                    Map<String,String> tempTownCnMap;
                    if (newMap.containsKey(shiCode)){
                        tempTownCnMap = newMap.get(shiCode);
                    }else {
                        tempTownCnMap = new HashMap<>();
                    }
                    tempTownCnMap.put(entryTown.getKey(),entryTown.getValue());
                    newMap.put(shiCode,tempTownCnMap);
                }

            }
        }
        SpiderDic.ADDRESS_CODE=newMap;
    }


    @Bean
    public SpringUtil getSpringUtil(){
        return new SpringUtil();
    }

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }

    /**
     * 错误页面的bean
     *
     * @return
     */
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return new EmbeddedServletContainerCustomizer() {

            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/error/401.html");
                ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN, "/error/403.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html");
                container.addErrorPages(error401Page, error403Page, error404Page, error500Page);
            }
        };
    }
}
